//: typeinfo/ClassInitialization.java
package typeinfo;
import java.util.*;
class Initable {
  static final int staticFinal = 47;//常数静态域，编译器常量
  static final int staticFinal2 =//运行时静态域，虽是常数，是非编译器常量
    ClassInitialization.rand.nextInt(1000);
  static {
    System.out.println("Initializing Initable");//静态块
  }
}

class Initable2 {
  static int staticNonFinal = 147;//非常数静态域
  static {
    System.out.println("Initializing Initable2");
  }
}

class Initable3 {
  static int staticNonFinal = 74;
  static {
    System.out.println("Initializing Initable3");
  }
}

public class ClassInitialization {
  public static Random rand = new Random(47);
  public static void main(String[] args) throws Exception {
    Class initable = Initable.class;//.class不触发初始化
    System.out.println("After creating Initable ref");
    // Does not trigger initialization:
    System.out.println(Initable.staticFinal);//常数静态域也不触发
    // Does trigger initialization:
    System.out.println(Initable.staticFinal2);//运行时，非编译器常量，初始化
    // Does trigger initialization:
    System.out.println(Initable2.staticNonFinal);//普通静态域，初始化
    Class initable3 = Class.forName("Initable3");//forName调用，初始化
    System.out.println("After creating Initable3 ref");
    System.out.println(Initable3.staticNonFinal);
  }
} /* Output:
After creating Initable ref
47
Initializing Initable
258
Initializing Initable2
147
Initializing Initable3
After creating Initable3 ref
74
*///:~
